package com.finance.rule.computer;

import com.alibaba.fastjson.JSONObject;
import com.finance.base.constant.BaseConstants;
import com.finance.base.enums.MajorTypeEnum;
import com.finance.base.exception.BaseException;
import com.finance.report.bean.pojo.LogisticsData;
import com.finance.report.service.ILogisticsDataService;
import net.sf.json.JSONArray;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 混合解析规则
 * 要求按照其中一条逻辑判断为主件即可
 * 1、以后缀判断为主件
 * 2、相同运单号以第一单为主件
 */
@Component
@Scope("prototype")
public class SameBusinessCodeAndWaybillNumSeperatorSuffixAnalyzer implements IMajorAnalyzer ,Serializable {
    @Resource
    private ILogisticsDataService logisticsDataService;

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    private String majorSuffix = "";

    private String separator = "";

    @Override
    public boolean isSingle(LogisticsData logisticsData) {
        return CollectionUtils.isEmpty(this.getOtherParts(logisticsData));
    }


    @Override
    public MajorTypeEnum majorTyeOf(LogisticsData logisticsData) {
        boolean major =  logisticsData.getWaybillNum().trim().endsWith(this.separator+ majorSuffix);
        if(major) {
            return MajorTypeEnum.MAJOR;
        }
        if(this.isSingle(logisticsData)) {
            return MajorTypeEnum.MAJOR;
        }
        List<LogisticsData> list = this.getOtherParts(logisticsData);
        list.add(logisticsData);
        this.logger.info("{}的其他部分{}",logisticsData.getWaybillNum(), JSONArray.fromObject(list).toString());
        Collections.sort(list, new Comparator<LogisticsData>() {
            @Override
            public int compare(LogisticsData o1, LogisticsData o2) {
                int result = o1.getWaybillNum().compareTo(o2.getWaybillNum());
                SameBusinessCodeAndWaybillNumSeperatorSuffixAnalyzer.this.logger.info("{}和{}比较结果{}",new String[]{o1.getWaybillNum(),o2.getWaybillNum(),result+""});
                return result;
            }
        });

        return list.get(0).equals(logisticsData) ? MajorTypeEnum.MAJOR:MajorTypeEnum.DRAG;
    }

    @Override
    public List<LogisticsData> getOtherParts(LogisticsData logisticsData) {

        this.logger.info("获取{}的其他部分",logisticsData.getWaybillNum());
        String tableName = this.logisticsDataService.getDataTableName(logisticsData.getSignTime());
        List<LogisticsData> list = this.logisticsDataService.getByBusinessCode(logisticsData.getBusinessCode(),tableName);
        if(CollectionUtils.isEmpty(list)|| list.size()<=1 ) {
            return new ArrayList<LogisticsData>();
        }
        List<LogisticsData> result = new ArrayList<>();
        result.addAll(list);        result.remove(logisticsData);


        this.logger.info("{}的其他部分数量为{}",logisticsData.getWaybillNum(),list.size());
        return result;
    }

    @Override
    public void init(String json) {
        if(!StringUtils.isEmpty(json)) {
            JSONObject jsonObject = JSONObject.parseObject(json);
            majorSuffix = jsonObject.getString("majorSuffix");
            separator = jsonObject.getString("separator");
        }else{
            throw  new BaseException(BaseConstants.ERR_INIT_JSON_IS_NULL);
        }

    }
}
